package org.example.Impl;
import org.example.dao.NewsDao;
import org.example.po.News;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class NewsImpletation implements NewsDao {
    @Override
    /**
     * 新增新闻
     */
    public int addNews(News news) throws Exception {
        PreparedStatement stmt=null;
        Connection conn =null;
        int flag = 0;
        try {
            //  1.注册数据库的驱动
            Class.forName("com.mysql.jdbc.Driver");
            //  2.创建对象连接
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/demo01","jdbc","jdbc123");
            //  3.通过conn连接获取statement对象
            String sql="insert into news values(null,?,?)";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1,news.getTitle());
            stmt.setString(2,news.getContent());
            //  4.使用处理对象执行sql语句
            stmt.executeUpdate();
            flag = 1;
        }catch (Exception e){
            throw new Exception(e.getMessage());
        }finally {
            if(stmt!=null) {
                stmt.close();
            }
            if(conn!=null) {
                conn.close();
            }
        }
        return flag;
    }

    @Override
    public int deleteNewsByID(int Id) throws Exception {
        PreparedStatement stmt=null;
        Connection conn =null;
        int flag = 0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/demo01","jdbc","jdbc123");
            //  3.通过conn连接获取statement对象
            String sql="delete from news where Id=?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1,Id);
            stmt.executeUpdate();
            flag = 1;
        }catch (Exception e){
            throw new Exception(e.getMessage());
        }finally {
            if(stmt!=null) {
                stmt.close();
            }
            if(conn!=null) {
                conn.close();
            }
        }
        return flag;
    }

    @Override
    public News findNewsById(int Id) throws Exception {
        PreparedStatement stmt=null;
        Connection conn =null;
        ResultSet rs=null;
        News news = new News();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/demo01","jdbc","jdbc123");
            String sql="select * from news where Id=?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1,Id);
            rs = stmt.executeQuery();
            if(rs.next()){
               news.setTitle(rs.getString("title"));
               news.setContent(rs.getString("content"));
            }
        }catch (Exception e){
            throw new Exception(e.getMessage());
        }finally {
            if(stmt!=null) {
                stmt.close();
            }
            if(conn!=null) {
                conn.close();
            }
        }
        return news;
    }

    @Override
    public List<News> findAllNews() throws Exception {
        PreparedStatement stmt=null;
        Connection conn =null;
        ResultSet rs=null;
        List<News> newsList = new ArrayList<News>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/demo01","jdbc","jdbc123");
            String sql="select * from news";
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while(rs.next()){
                News news = new News();
                news.setId(rs.getInt("Id"));
                news.setTitle(rs.getString("title"));
                news.setContent(rs.getString("content"));
                newsList.add(news);
            }
        }catch (Exception e){
            throw new Exception(e.getMessage());
        }finally {
            if(stmt!=null) {
                stmt.close();
            }
            if(conn!=null) {
                conn.close();
            }
        }
        return newsList;
    }
}
